package com.fang.tcp;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server extends Thread{
    private ServerSocket serverSocket;
    public Server() {
        super("服务器：");
    }

    @Override
    public void run() {

            try {
             serverSocket = new ServerSocket(5543);
                Socket socket = serverSocket.accept();
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                while(true) {
                    ////////////////////////////////接收数据////////////////////////////////////////
                    String strSou = br.readLine();
                    System.out.println(strSou);

                    /////////////////////////////////发送数据/////////////////////////////////////////////

                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    Scanner sc = new Scanner(System.in);
                    String strMsg = sc.next();
                    System.out.println(this.getName() + strMsg);
                    bw.write(Thread.currentThread().getName() + strMsg);
                    Thread.sleep(1000);
                    bw.newLine();
                    bw.flush();

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}
